package com.alibaba.algo.domain.user;

import com.alibaba.algo.cooperation.usercomponent.vo.res.user.EmployeeInfo;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.OrgGroupVO;
import com.alibaba.algo.core.base.BaseDomain;
import com.alibaba.algo.core.base.YesOrNo;
import com.alibaba.algo.dao.user.SysOrgGroupMapper;
import com.alibaba.algo.dtos.user.EmployeeUserOrgDTO;
import com.alibaba.algo.entity.user.SysEmployeeGroup;
import com.alibaba.algo.entity.user.SysGroup;
import com.alibaba.algo.entity.user.SysOrgGroup;
import com.alibaba.algo.util.DateConverterUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Component
public class SysOrgGroupDomain extends BaseDomain<SysOrgGroup> {

    @Resource
    private SysOrgGroupMapper sysOrgGroupMapper;

    /**
     * 批量新增用户组和组织的关联关系
     *
     * @param groupId        用户组标识
     * @param orgIdList      组织列表
     */
    public void batchInsert(Long groupId, List<Long> orgIdList,String userName) {
        LocalDateTime cutDateTime = DateConverterUtils.asLocalDateTime(new Date());
        List<SysOrgGroup> sysOrgGroupList = new ArrayList<>();
        for (Long orgId : orgIdList) {
            SysOrgGroup sysOrgGroup = SysOrgGroup.builder().build();
            sysOrgGroup.setOrgId(orgId);
            sysOrgGroup.setGroupId(groupId);
            sysOrgGroup.setIsDeleted(YesOrNo.N.name());
            sysOrgGroup.setGmtCreated(cutDateTime);
            sysOrgGroup.setGmtModified(cutDateTime);
            sysOrgGroup.setCreator(userName);
            sysOrgGroup.setModifier(userName);
            sysOrgGroupList.add(sysOrgGroup);
        }
        sysOrgGroupMapper.batchInsert(sysOrgGroupList);
    }

    public List<OrgGroupVO> selectOrgGroupListByGroupId(Long groupId) {
        return sysOrgGroupMapper.selectOrgIdListByGroupId(groupId);
    }

    public List<EmployeeInfo> selectEmployeeListByOrgIdList(List<Long> orgIdList) {
        return sysOrgGroupMapper.selectEmployeeListByOrgIdList(orgIdList);
    }

    public List<EmployeeInfo> selectEmployeeListByOrgIdsAndEmployeeUser(EmployeeUserOrgDTO employeeUserOrgDTO){
        return sysOrgGroupMapper.selectEmployeeListByOrgIdsAndEmployeeUser(employeeUserOrgDTO);
    }

    public Integer countEmployeeListByOrgIds(EmployeeUserOrgDTO employeeUserOrgDTO){
        return sysOrgGroupMapper.countEmployeeListByOrgIds(employeeUserOrgDTO);
    }

    public List<SysOrgGroup> selectByOrgIdList(List<Long> orgIdList){
        return sysOrgGroupMapper.selectByOrgIdList(orgIdList);
    }

    public void deleteByGroupId(Long groupId){
        sysOrgGroupMapper.deleteByGroupId(groupId);
    }

    public List<SysGroup> selectGroupListByOrgIdList(List<Long> orgIdList){
         return sysOrgGroupMapper.selectGroupListByOrgIdList(orgIdList);
    }

    public Integer updateByOrgIdGroupId(SysOrgGroup sysOrgGroup){
        return sysOrgGroupMapper.updateByOrgIdGroupId(sysOrgGroup);
    }

    public List<SysOrgGroup> selectOrgGroupListByGroupIds(List<Long> groupIdList, String isDeleted) {
        return sysOrgGroupMapper.selectOrgGroupListByGroupIds(groupIdList, isDeleted);
    }

}
